using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UMC.Data;
using UMC.Web;


namespace UMC.WorkFlow.Subassembly
{
    /// <summary>
    /// 
    /// </summary>
    [Apiumc("WorkFlow", "HttpSetting", Auth = WebAuthType.Admin, Desc = "流程HTTP设置")]
    public class HttpSettingDesign : DesignActivity
    {
        protected override object SetValue(PropertyInfo property, object value, DesignAttribute attr)
        {
            if (property.PropertyType == typeof(GetSetting))
            {
                var sett = value as GetSetting ?? new GetSetting();
                var v = this.AsyncDialog("HttpSetting", g =>
                {
                    var form = new UIFormDialog() { Title = attr.Caption };
                    form.AddCheckBox("事件", "CommandType", "0")//.Put("保存", "1", (sett.CommandType & CommandType.Save) > 0)
                    .Put("申请", "2", (sett.CommandType & CommandType.Submit) > 0)
                    .Put("驳回", "4", (sett.CommandType & CommandType.Reject) > 0)
                    .Put("挂起", "8", (sett.CommandType & CommandType.Lock) > 0)
                    .Put("通过", "16", (sett.CommandType & CommandType.Approval) > 0)
                    .Put("加签", "64", (sett.CommandType & CommandType.Apostille) > 0)
                    .Put("终止", "32", (sett.CommandType & CommandType.Terminate) > 0)
                    .Put("完结", "512", (((int)sett.CommandType) & 512) > 0);

                    form.AddText("网址", "Url", sett.Url).NotRequired();
                    form.Submit("确认", "WorkFlow.Design");
                    if (String.IsNullOrEmpty(attr.Explain) == false)
                    {
                        form.AddFooter(attr.Explain);
                        //var desc = new UMC.Web.UI.UIDesc(attr.Explain);
                        //desc.Style.Color(0x999).Size(12);
                        //form.Add(desc);
                    }
                    return form;
                });
                sett.CommandType = CommandType.Undefine;
                var vs = v["CommandType"].Split(',');
                foreach (var c in vs)
                {
                    sett.CommandType = sett.CommandType | (CommandType)UMC.Data.Utility.EnumParse(c, CommandType.Undefine);
                }
                var url = v["Url"];
                if (String.IsNullOrEmpty(url) == false)
                {
                    try
                    {
                        sett.Url = new Uri(url).AbsoluteUri;
                    }
                    catch
                    {
                        this.Prompt("网址格式不正确");
                    }
                }
                else
                {
                    sett.Url = String.Empty;
                }
                return sett;
            }
            else
            {
                var sett = value as HttpSetting ?? new HttpSetting();
                var v = this.AsyncDialog("HttpSetting", g =>
                {
                    var form = new UIFormDialog() { Title = attr.Caption };
                    form.AddCheckBox("事件", "CommandType", "0")//.Put("保存", "1", (sett.CommandType & CommandType.Save) > 0)
                    .Put("申请", "2", (sett.CommandType & CommandType.Submit) > 0)
                    .Put("驳回", "4", (sett.CommandType & CommandType.Reject) > 0)
                    .Put("挂起", "8", (sett.CommandType & CommandType.Lock) > 0)
                    .Put("通过", "16", (sett.CommandType & CommandType.Approval) > 0)
                    .Put("加签", "64", (sett.CommandType & CommandType.Apostille) > 0)
                    .Put("终止", "32", (sett.CommandType & CommandType.Terminate) > 0)
                    .Put("完结", "512", (((int)sett.CommandType) & 512) > 0);

                    form.AddOption("接口管理员", "Manger", sett.Manger, sett.Manger).Command("WorkFlow", "Username");
                    form.AddText("网址", "Url", sett.Url).NotRequired();
                    form.Submit("确认",  "WorkFlow.Design");
                    if (String.IsNullOrEmpty(attr.Explain) == false)
                    {
                        form.AddFooter(attr.Explain);
                    }
                    return form;
                });
                sett.Manger = v["Manger"];
                sett.CommandType = CommandType.Undefine;
                var vs = v["CommandType"].Split(',');
                foreach (var c in vs)
                {
                    sett.CommandType = sett.CommandType | (CommandType)UMC.Data.Utility.EnumParse(c, CommandType.Undefine);
                }
                var url = v["Url"];
                if (String.IsNullOrEmpty(url) == false)
                {
                    try
                    {
                        sett.Url = new Uri(url).AbsoluteUri;
                    }
                    catch
                    {
                        this.Prompt("网址格式不正确");
                    }
                }
                else
                {
                    sett.Url = String.Empty;
                }
                return sett;
            }

        }
    }
}
